// pages/home/home.js
import api from "../../utils/api.js";
import config from "../../utils/config.js";
const crypto = require('../../utils/crypto.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPrivacy: false,
    index:0,
    grades:[],
    items: [{
        "icon": "一",
        "title": "一年级",
        "detail": "语文、数学",
        "courseList":[
          {
            "id": 1101,
            "grade": 1,
            "gradeDetail": "一年级",
            "semester": 1,
            "title": "上",
            "course": "语文"
          },
          {
            "id": 1102,
            "grade": 1,
            "gradeDetail": "一年级",
            "semester": 1,
            "title": "上",
            "course": "数学"
          },
          {
            "id": 1201,
            "grade": 1,
            "gradeDetail": "一年级",
            "semester": 2,
            "title": "下",
            "course": "语文"
          },
          {
            "id": 1202,
            "grade": 1,
            "gradeDetail": "一年级",
            "semester": 2,
            "title": "下",
            "course": "数学"
          }
        ]
      },
      {
        "icon": "二",
        "title": "二年级",
        "detail": "储备知识,开阔视野",
        "courseList":[{
          "id": 2101,
          "grade": 2,
          "gradeDetail": "二年级",
          "semester": 1,
          "title": "上",
          "course": "语文"
        },
        {
          "id": 2102,
          "grade": 2,
          "gradeDetail": "二年级",
          "semester": 1,
          "title": "上",
          "course": "数学"
        },
        {
          "id": 2201,
          "grade": 2,
          "gradeDetail": "二年级",
          "semester": 2,
          "title": "下",
          "course": "语文"
        },
        {
          "id": 2202,
          "grade": 2,
          "gradeDetail": "二年级",
          "semester": 2,
          "title": "下",
          "course": "数学"
        }]
      },
      {
        "icon": "三",
        "title": "三年级",
        "detail": "拓展词汇,字字珠玑",
        "courseList":[{
          "id": 3101,
          "grade": 3,
          "gradeDetail": "三年级",
          "semester": 1,
          "title": "上",
          "course": "语文"
        },
        {
          "id": 3102,
          "grade": 3,
          "gradeDetail": "三年级",
          "semester": 1,
          "title": "上",
          "course": "数学"
        },
        {
          "id": 3201,
          "grade": 3,
          "gradeDetail": "三年级",
          "semester": 2,
          "title": "下",
          "course": "语文"
        },
        {
          "id": 3202,
          "grade": 3,
          "gradeDetail": "三年级",
          "semester": 2,
          "title": "下",
          "course": "数学"
        }]
      },
      {
        "icon": "四",
        "title": "四年级",
        "detail": "拓展词汇,字字珠玑",
        "courseList":[{
          "id": 4101,
          "grade": 4,
          "gradeDetail": "四年级",
          "semester": 1,
          "title": "上",
          "course": "语文"
        },
        {
          "id": 4102,
          "grade": 4,
          "gradeDetail": "四年级",
          "semester": 1,
          "title": "上",
          "course": "数学"
        },
        {
          "id": 4201,
          "grade": 4,
          "gradeDetail": "四年级",
          "semester": 2,
          "title": "下",
          "course": "语文"
        },
        {
          "id": 4202,
          "grade": 4,
          "gradeDetail": "四年级",
          "semester": 2,
          "title": "下",
          "course": "数学"
        }]
      },
      {
        "icon": "五",
        "title": "五年级",
        "detail": "拓展词汇,字字珠玑",
        "courseList":[{
          "id": 5101,
          "grade": 5,
          "gradeDetail": "五年级",
          "semester": 1,
          "title": "上",
          "course": "语文"
        },
        {
          "id": 5102,
          "grade": 5,
          "gradeDetail": "五年级",
          "semester": 1,
          "title": "上",
          "course": "数学"
        },
        {
          "id": 5201,
          "grade": 5,
          "gradeDetail": "五年级",
          "semester": 2,
          "title": "下",
          "course": "语文"
        },
        {
          "id": 5202,
          "grade": 5,
          "gradeDetail": "五年级",
          "semester": 2,
          "title": "下",
          "course": "数学"
        }]
      },
      {
        "icon": "六",
        "title": "六年级",
        "detail": "拓展词汇,字字珠玑",
        "courseList":[{
          "id": 6101,
          "grade": 6,
          "gradeDetail": "六年级",
          "semester": 1,
          "title": "上",
          "course": "语文"
        },
        {
          "id": 6102,
          "grade": 6,
          "gradeDetail": "六年级",
          "semester": 1,
          "title": "上",
          "course": "数学"
        },
        {
          "id": 6201,
          "grade": 6,
          "gradeDetail": "六年级",
          "semester": 2,
          "title": "下",
          "course": "语文"
        },
        {
          "id": 6202,
          "grade": 6,
          "gradeDetail": "六年级",
          "semester": 2,
          "title": "下",
          "course": "数学"
        }]
      }
    ],
    
    courseDatas: [],
    currentCouseList:null,
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var that = this;
    this.setData({
      index: e.detail.value,
      currentCouseList :  that.data.courseDatas[e.detail.value].Books
    })
  },
  onOpen(event) {
    console.log("---->", event.currentTarget.dataset.id);
    wx.navigateTo({
      url: '/pages/home/courseList/courseList?data='+JSON.stringify(event.currentTarget.dataset.id.courseList)
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // wx.getPrivacySetting({
    //   success: res => {
    //     console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
    //     if (res.needAuthorization) {
    //       // 需要弹出隐私协议
    //       this.setData({
    //         showPrivacy: true
    //       })
    //     } else {
    //       // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
    //       // wx.getUserProfile()
    //       // wx.chooseMedia()
    //       // wx.getClipboardData()
    //       // wx.startRecord()
    //     }
    //   },
    //   fail: () => {},
    //   complete: () => {}
    // })



    const code = wx.getStorageSync('Code');
    console.log("bookCode:"+code);
    const token = api.getToken(code);
    const tokenJson = JSON.stringify(token);
    const encrypToken = crypto.encryptData(tokenJson);
    wx.setStorageSync('Token', encrypToken);
    api.getBookList().then(response => {
      console.log('Book成功', response);
      const grades = response.ResultData.map(item=>item.Grade);
      console.log(grades);
      
      response.ResultData.forEach(dataItem=>{
        dataItem.Books.forEach(book => {
          book.CoverPhotoUrl= config.baseUrl+book.CoverPhotoUrl
        });
      })
      const books = response.ResultData[0].Books;
      console.log(books);
      const that = this;
      this.setData({
        grades:grades,
        courseDatas:response.ResultData,
        currentCouseList: books
      })
    }).catch(error => {
      console.error('book失败', error);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  handleAgreePrivacyAuthorization() {
    // 用户同意隐私协议事件回调
    // 用户点击了同意，之后所有已声明过的隐私接口和组件都可以调用了
    // wx.getUserProfile()
    // wx.chooseMedia()
    // wx.getClipboardData()
    // wx.startRecord()
  },
  handleOpenPrivacyContract() {
    // 打开隐私协议页面
    wx.openPrivacyContract({
      success: () => {}, // 打开成功
      fail: () => {}, // 打开失败
      complete: () => {}
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const code = wx.getStorageSync('Code');
    console.log("bookCode:"+code);
    const token = api.getToken(code);
    const tokenJson = JSON.stringify(token);
    const encrypToken = crypto.encryptData(tokenJson);
    wx.setStorageSync('Token', encrypToken);
    api.getBookList().then(response => {
      console.log('Book成功', response);
    }).catch(error => {
      console.error('book失败', error);
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})